package com.avic.szzx.service;

import com.alibaba.fastjson.JSONObject;
import com.avic.szzx.config.NebulaTemplate;
import com.avic.szzx.controller.bean.quality_tracing.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.*;

@Service
@Slf4j
public class NebulaService {
    @Resource
    NebulaTemplate nebulaTemplate;

    /**
     * 新增模型实例数据
     * @param data
     * @param tag
     * @return
     */
    public boolean addVertexData(LinkedHashMap<String, Object> data, String tag, String idx) {
        log.info("图数据库写入数据准备：",tag);
        int i = 0;
//        String idx = "";
        StringBuffer sql = new StringBuffer();
        sql.append("insert vertex " + tag + "(");
        StringBuffer keys = new StringBuffer();
        StringBuffer values = new StringBuffer();
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key.equals("creation_date")){
                value = String.valueOf(new Date().getTime());
            }
//            if (i != 0) {
                if (i != data.size() - 1) {
                    keys.append(key + ",");
                    values.append("\"" + value + "\",");
                } else {
                    keys.append(key);
                    values.append("\"" + value + "\"");
                }
//            } else {
//                //idx = value.toString();
//            }
            i++;
        }
        StringBuffer append = sql.append(keys).append(") values ").append("\"").append(idx).append("\"").append(":(").append(values).append(");");
        System.out.println("nGQL: " +  append.toString());
        JSONObject nebulaResult = nebulaTemplate.executeObject(String.valueOf(append));
        boolean result = false;
        if (null!=nebulaResult){
            result = true;
        }
        return result;
    }

    /**
     * 新增模型实例数据
     * @param data
     * @param tag
     * @return
     */
    public boolean addVertexData(LinkedHashMap<String, Object> data, String tag) {
        log.info("图数据库写入数据准备：",tag);
        int i = 0;
        String idx = "";
        StringBuffer sql = new StringBuffer();
        sql.append("insert vertex " + tag + "(");
        StringBuffer keys = new StringBuffer();
        StringBuffer values = new StringBuffer();
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key.equals("creation_date")){
                value = String.valueOf(new Date().getTime());
            }
            if (i != 0) {
                if (i != data.size() - 1) {
                    keys.append(key + ",");
                    values.append("\"" + value + "\",");
                } else {
                    keys.append(key);
                    values.append("\"" + value + "\"");
                }
            } else {
                idx = value.toString();
            }
            i++;
        }
        StringBuffer append = sql.append(keys).append(") values ").append("\"").append(idx).append("\"").append(":(").append(values).append(");");
        System.out.println("nGQL: " +  append.toString());
        JSONObject nebulaResult = nebulaTemplate.executeObject(String.valueOf(append));
        boolean result = false;
        if (null!=nebulaResult){
            result = true;
        }
        return result;
    }

    /**
     * batch add vertex data
     * @param datas
     * @param tag
     * @return
     */
    public boolean addVertexDatas(List<LinkedHashMap<String, Object>> datas, String tag) {
        log.info("图数据库写入数据准备：", tag);
        StringBuffer sql = new StringBuffer();
        sql.append("insert vertex " + tag + "(");
        StringBuffer keys = new StringBuffer();
        int num = 0;
        for (LinkedHashMap<String, Object> data : datas) {
            String idx = "";
            StringBuffer values = new StringBuffer();
            StringBuffer vertex = new StringBuffer();
            int i = 0;
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (key.equals("creation_date")) {
                    value = String.valueOf(new Date().getTime());
                }
                if (i != 0) {
                    if (i != data.size() - 1) {
                        if (num == 0) {
                            keys.append(key + ",");
                        }
                        values.append("\"" + value + "\",");
                    } else {
                        if (num == 0) {
                            keys.append(key);
                        }
                        values.append("\"" + value + "\"");
                    }
                } else {
                    idx = value.toString();
                }
                i++;
            }
            if (num == 0) {
                sql.append(keys).append(") VALUES ");
                vertex.append(idx).append(":(").append(values).append("),");
            } else if (num != datas.size() - 1) {
                vertex.append(idx).append(":(").append(values).append("),");
            } else {
                vertex.append(idx).append(":(").append(values).append(");");
            }
            sql.append(vertex);
            num++;
        }
        JSONObject nebulaResult = nebulaTemplate.executeObject(String.valueOf(sql));
        boolean result = false;
        if (null != nebulaResult) {
            result = true;
        }
        return result;
    }
    /**
     * 新增模型关系数据
     * @param data
     * @param edge
     * @return
     */
    public boolean addEdgeData(Map<String, Object> data,String edge){
            int i = 0;
            StringBuffer src_vid = new StringBuffer();
            StringBuffer dst_vid = new StringBuffer();
            StringBuffer sql = new StringBuffer();
            sql.append("insert edge "+edge+"(");
            StringBuffer keys = new StringBuffer();
            StringBuffer values = new StringBuffer();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (key.equals("creation_date")){
                    value = String.valueOf(new Date().getTime());
                }
                if (key.equals("src_vid")){
                    src_vid.append("\"" + value + "\"");
                }
                if (key.equals("dst_vid")){
                    dst_vid.append("\"" + value + "\"");
                }
//                if (i == 0) {
//                    src_vid.append("\"" + value + "\"");
//                } else if (i == 1) {
//                    dst_vid.append("\"" + value + "\"");
//                } else if (i > 1) {
//                    if (i != data.size() - 1) {
//                        keys.append(key + ",");
//                        values.append("\"" + value + "\",");
//                    } else {
//                        keys.append(key);
//                        values.append("\"" + value + "\"");
//                    }
//                    System.out.println(key + "->>>" + value);
//                }
                i++;
            }
        StringBuffer append = sql.append(keys).append(") values ").append(src_vid).append("->").append(dst_vid).append(":(").append(values).append(");");
        System.out.println("nGQL: " +  append.toString());
        JSONObject nebulaResult = nebulaTemplate.executeObject(String.valueOf(append));
        boolean result = false;
        if (null!=nebulaResult){
            result = true;
        }
        return result;
    }

    /**
     * 新增模型关系数据
     * @param data
     * @param edge
     * @return
     */
    public boolean addEdgeDataOld(Map<String, Object> data,String edge){
        int i = 0;
        StringBuffer src_vid = new StringBuffer();
        StringBuffer dst_vid = new StringBuffer();
        StringBuffer sql = new StringBuffer();
        sql.append("insert edge "+edge+"(");
        StringBuffer keys = new StringBuffer();
        StringBuffer values = new StringBuffer();
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key.equals("creation_date")){
                value = String.valueOf(new Date().getTime());
            }
            if (key.equals("creation_date")){
                value = String.valueOf(new Date().getTime());
            }
            if (i == 0) {
                src_vid.append("\"" + value + "\"");
            } else if (i == 1) {
                dst_vid.append("\"" + value + "\"");
            } else if (i > 1) {
                if (i != data.size() - 1) {
                    keys.append(key + ",");
                    values.append("\"" + value + "\",");
                } else {
                    keys.append(key);
                    values.append("\"" + value + "\"");
                }
                System.out.println(key + "->>>" + value);
            }
            i++;
        }
        StringBuffer append = sql.append(keys).append(") values ").append(src_vid).append("->").append(dst_vid).append(":(").append(values).append(");");
        System.out.println("nGQL: " +  append.toString());
        JSONObject nebulaResult = nebulaTemplate.executeObject(String.valueOf(append));
        boolean result = false;
        if (null!=nebulaResult){
            result = true;
        }
        return result;
    }
    public boolean updateVertexData(LinkedHashMap<String, Object> data, String tag) {
        log.info("图数据库修改数据准备：",tag);
        String idx = "";
        int i = 0;
        StringBuffer sql = new StringBuffer();
        StringBuffer part = new StringBuffer();
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key.equals("creation_date")){
                value = String.valueOf(new Date().getTime());
            }
            if (i != 0) {
                if(i==1){
                    part.append(" SET " + key + "=");
                    part.append("\"" + value + "\",");
                }
                else if(i != data.size() - 1) {
                    part.append(" " + key + "=");
                    part.append("\"" + value + "\",");
                } else {
                    part.append(" " + key + "=");
                    part.append("\"" + value + "\"");
                }
                log.info(key + "->>>" + value);
            } else {
                idx = value.toString();
            }
            i++;
        }
        StringBuffer append = sql.append("UPDATE VERTEX ON " + tag).append(" \"" + idx + "\"").append(part);
        JSONObject nebulaResult = nebulaTemplate.executeObject(String.valueOf(append));
        boolean result = false;
        if (null!=nebulaResult){
            result = true;
        }
        return result;
    }
    public JSONObject selectVertexData(String name) {
        String sql = "MATCH (v:" + name + ") RETURN v;";
        JSONObject nebulaResult = nebulaTemplate.queryObject(sql);
        System.out.println("------selectVertexData nebulaResult" + nebulaResult.toString());
        return nebulaResult;
    }
    public JSONObject selectEdgeData(String name) {
        String sql = "MATCH ()-[e:"+name+"]->() RETURN e;";
        JSONObject nebulaResult = nebulaTemplate.queryObject(sql);
        return nebulaResult;
    }
    public JSONObject selectExploreData(String value,int layer) {
        String sql = "GO " + layer + " STEPS FROM \"" + value + "\" OVER * BIDIRECT YIELD edge AS e;";
        log.info("sql:"+sql);
        JSONObject jsonObject = nebulaTemplate.executeNGQL(sql);
        return jsonObject;
    }
    public JSONObject selectVertexDataTotal(String name) {
        String sql = "MATCH (v:" + name + ") RETURN count(v) as total;";
        JSONObject nebulaResult = nebulaTemplate.executeObject(sql);
        return nebulaResult;
    }
    public JSONObject selectEdgeDataTotal(String name) {
        String sql = "MATCH (v:" + name + ") RETURN count(e) as total;";
        JSONObject nebulaResult = nebulaTemplate.executeObject(sql);
        return nebulaResult;
    }
    public Class<?> getVertexClass(String tag) {
        Class<?> result = null;
        switch (tag) {
            case "equipment_information_correspondence":
                result = EquipmentInformationCorrespondence.class;
                break;
            case "processing_record":
                result = ProcessRecord.class;
                break;
            case "quality_issue":
                result = QualityIssue.class;
                break;
            case "quality_record":
                result = QualityRecord.class;
                break;
            case "tooling_information":
                result = ToolingInformation.class;
                break;
            case "user_information":
                result = UserInformation.class;
                break;
        }
        return result;
    }
        public boolean deleteVertexData (LinkedHashMap < String, Object > data){
            log.info("图数据库删除数据准备");
            StringBuffer sql = new StringBuffer();
            sql.append("DELETE VERTEX \"").append(data.get("id").toString()).append("\";");
            JSONObject nebulaResult = nebulaTemplate.executeObject(sql.toString());
            boolean result = false;
            if (null!=nebulaResult){
                result = true;
            }
            return result;
        }

        public boolean deleteEdgeData (LinkedHashMap < String, Object > data, String tag){
            log.info("图数据库删除数据准备");
            StringBuffer sql = new StringBuffer();
            int i = 0;
            String src_vid = "";
            String dst_vid = "";
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                Object value = entry.getValue();
                if (i == 1) {
                    src_vid = "\"" + value + "\"";
                } else if (i == 2) {
                    dst_vid = "\"" + value + "\"";
                }
            }
            sql.append("DELETE EDGE \"").append(tag).append(src_vid).append("->").append(dst_vid).append("@0;");
            JSONObject nebulaResult = nebulaTemplate.executeObject(sql.toString());
            boolean result = false;
            if (null!=nebulaResult){
                result = true;
            }
            return result;
        }
 }
